<?php
declare (strict_types = 1);

namespace app\controller;

use think\Request;

class Pdd
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }

    /**
     * 获取拼多多订单列表
     * @return \think\Response
     */
    public function getOrderList()
    {
        $config = config('pdd');
        $accessToken = $this->getAccessToken($config);
        if (!$accessToken) {
            return json(['error' => '获取access_token失败'], 500);
        }
        // 拼多多订单列表查询接口
        $url = "https://gw-api.pinduoduo.com/api/router";
        $data = [
            'type' => 'pdd.order.list.get',
            'client_id' => $config['client_id'],
            'access_token' => $accessToken,
            'timestamp' => time(),
            'version' => 'V1',
            'page' => 1,
            'page_size' => 20
        ];
        $response = $this->httpPost($url, $data);
        return json($response);
    }

    /**
     * 获取拼多多access_token
     * @param array $config
     * @return string|null
     */
    private function getAccessToken($config)
    {
        $url = "https://gw-api.pinduoduo.com/api/router";
        $data = [
            'type' => 'pdd.pop.auth.token.create',
            'client_id' => $config['client_id'],
            'client_secret' => $config['client_secret'],
            'timestamp' => time(),
            'version' => 'V1'
        ];
        $result = $this->httpPost($url, $data);
        if (isset($result['access_token'])) {
            return $result['access_token'];
        }
        return null;
    }

    /**
     * 发送POST请求
     */
    private function httpPost($url, $data)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data, JSON_UNESCAPED_UNICODE));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
        $output = curl_exec($ch);
        curl_close($ch);
        return json_decode($output, true);
    }
}
